﻿Imports System.Collections
Imports sl3d.core

#Region "sl3matrix base class"
Public Class sl3matrix
#Region "Class Methods"
    ''' <summary>
    ''' Adds 2 Matrices together of equal size
    ''' </summary>
    ''' <param name="m1">Multi-Array of Double values</param>
    ''' <param name="m2">Multi-Array of Double values</param>
    ''' <exception cref="FormatException ">Thrown if the 2 matrices passed are not equal in rows and columns</exception>
    ''' <exception cref="ArrayTypeMismatchException">Thrown if the resulting matrix is not equal to the matrices passed to the method.</exception>
    ''' <returns>Multi-Array of Double</returns>
    ''' <remarks></remarks>
    Public Shared Function AddMatrix(ByRef m1(,) As Double, ByRef m2(,) As Double) As Double(,)
        Try
            If Not (UBound(m1, 1) = UBound(m2, 1) And UBound(m1, 2) = UBound(m2, 2)) Then
                Throw New FormatException("One of the Matrices passed to the function is not the same size as the other")
            End If

            Dim newMatrix(UBound(m1, 1), UBound(m1, 2)) As Double

            For i As Integer = 0 To UBound(m1, 1)
                For j As Integer = 0 To UBound(m1, 2)
                    newMatrix(i, j) = m1(i, j) + m2(i, j)
                Next
            Next

            If (UBound(newMatrix, 1) = UBound(m1, 1)) And (UBound(newMatrix, 2) = UBound(m1, 2)) Then
                Return newMatrix
            Else
                Throw New ArrayTypeMismatchException("Resulting Matrix is not equal in size to the original Matrices")
            End If
        Catch ex As Exception
            MessageBox.Show("Matrix Addition function failed due to the following problem:" & vbCrLf & ex.InnerException.ToString & " : " & ex.Message, "Matrix Addition Error", MessageBoxButton.OK)
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Subtract 2 Matrices of equal size
    ''' </summary>
    ''' <param name="m1">Multi-Array of Double values</param>
    ''' <param name="m2">Multi-Array of Double values</param>
    ''' <exception cref="FormatException ">Thrown if the 2 matrices passed are not equal in rows and columns</exception>
    ''' <exception cref="ArrayTypeMismatchException">Thrown if the resulting matrix is not equal to the matrices passed to the method.</exception>
    ''' <returns>Multi-Array of Double</returns>
    ''' <remarks></remarks>
    Public Shared Function SubMatrix(ByRef m1(,) As Double, ByRef m2(,) As Double) As Double(,)
        Try
            If Not (UBound(m1, 1) = UBound(m2, 1) And UBound(m1, 2) = UBound(m2, 2)) Then
                Throw New FormatException("One of the Matrices passed to the function is not the same size as the other")
            End If

            Dim newMatrix(UBound(m1, 1), UBound(m1, 2)) As Double

            For i As Integer = 0 To UBound(m1, 1)
                For j As Integer = 0 To UBound(m1, 2)
                    newMatrix(i, j) = m1(i, j) - m2(i, j)
                Next
            Next

            If (UBound(newMatrix, 1) = UBound(m1, 1)) And (UBound(newMatrix, 2) = UBound(m1, 2)) Then
                Return newMatrix
            Else
                Throw New ArrayTypeMismatchException("Resulting Matrix is not equal in size to the original Matrices")
            End If
        Catch ex As Exception
            MessageBox.Show("Matrix Subtraction function failed due to the following problem:" & vbCrLf & ex.InnerException.ToString & " : " & ex.Message, "Subtraction Matrix Error", MessageBoxButton.OK)
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Multiply a Matrix by a factor value
    ''' </summary>
    ''' <param name="m1">Multi-Array of Double values</param>
    ''' <param name="factor">Double value</param>
    ''' <returns>Multi-Array of Double values</returns>
    ''' <exception cref="Exception">Thrown if an error occurs within the function</exception>
    ''' <remarks></remarks>
    Public Shared Function ScalarMult(ByRef m1(,) As Double, ByVal factor As Double) As Double(,)
        Try
            Dim m1RowCnt As Integer = UBound(m1, 1)
            Dim m1ColCnt As Integer = UBound(m1, 2)

            Dim newMatrix(m1RowCnt, m1ColCnt) As Double

            For i As Integer = 0 To m1RowCnt
                For j As Integer = 0 To m1ColCnt
                    newMatrix(i, j) = m1(i, j) * factor
                Next
            Next

            Return newMatrix
        Catch ex As Exception
            MessageBox.Show("Matrix Scalar Multiplication function failed due to the following error: " & ex.InnerException.ToString, "Scalar Matrix Multiplication Error", MessageBoxButton.OK)
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Divide a Matrix by a factor value
    ''' </summary>
    ''' <param name="m1">Multi-Array of Double values</param>
    ''' <param name="factor">Double value</param>
    ''' <returns>Multi-Array of Double values</returns>
    ''' <exception cref="Exception">Thrown if an error occurs within the function</exception>
    ''' <remarks></remarks>
    Public Shared Function ScalarDiv(ByRef m1(,) As Double, ByVal factor As Double) As Double(,)
        Try
            If factor = 0 Then
                Throw New DivideByZeroException("Factor value cannot be zero!")
            End If

            Dim newMatrix(UBound(m1, 1), UBound(m1, 2)) As Double

            For i As Integer = 0 To UBound(m1, 1)
                For j As Integer = 0 To UBound(m1, 2)
                    newMatrix(i, j) = m1(i, j) / factor
                Next
            Next

            Return newMatrix
        Catch ex As Exception
            MessageBox.Show("Matrix Scalar Division function failed due to the following error: " & ex.InnerException.ToString & " : " & ex.Message, "Scalar Matrix Multiplication Error", MessageBoxButton.OK)
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Transpose a matrix.  This will take the existing rows and make them columns and columns will become rows
    ''' </summary>
    ''' <param name="m">Multi-Array of Double values</param>
    ''' <returns>Multi-Array of Double values</returns>
    ''' <remarks>
    ''' This was obtained from the DevX website (http://www.devx.com/vb2themax/Tip/19652)
    ''' </remarks>
    Public Shared Function Transpose(ByRef m(,) As Double) As Double(,)
        Try
            Dim res(m.GetUpperBound(1), m.GetUpperBound(0)) As Double

            For row As Integer = m.GetLowerBound(0) To m.GetUpperBound(0)
                For col As Integer = m.GetLowerBound(1) To m.GetUpperBound(1)
                    res(col, row) = m(row, col)
                Next
            Next

            Return res
        Catch ex As Exception
            ShowException("Matrix Transpose function errored out for the following reason:", ex, "Matrix Transpose Error")
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Mutliplies 2 matrices together to form 1 matrix
    ''' </summary>
    ''' <param name="m1">Multi-Array of Double values</param>
    ''' <param name="m2">Multi-Array of Double values</param>
    ''' <returns>Multi-Array of Double values</returns>
    ''' <remarks></remarks>
    Public Shared Function Concat(ByRef m1(,) As Double, ByRef m2(,) As Double) As Double(,)
        Try
            If Not ((m1.GetUpperBound(0) = m2.GetUpperBound(1)) And (m1.GetUpperBound(1) = m2.GetUpperBound(0))) Then
                Throw New FormatException("The two arrays do not equal in size")
            End If

            Dim newMatrix(m1.GetUpperBound(0), m1.GetUpperBound(1)) As Double

            For r As Integer = 0 To m1.GetUpperBound(0)
                For k As Integer = 0 To m1.GetUpperBound(0)
                    For c As Integer = 0 To m1.GetUpperBound(1)
                        newMatrix(r, k) += m1(r, c) * m2(c, k)
                    Next
                Next
            Next

            Return newMatrix
        Catch ex As Exception
            MessageBox.Show("The Concatenation function has errored out for the following reason:" & vbCrLf & ex.InnerException.ToString & " : " & ex.Message, "Concatenation Error", MessageBoxButton.OK)
            Return Nothing
        End Try
    End Function
#End Region
    Public Class Matrix2

    End Class
    Public Class Matrix3

    End Class
    Public Class Matrix4

    End Class
End Class
#End Region
